/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.tapestry.view;

import com.kcp.ko.fmtnmlib.service.search.SimpleSearch;
import org.apache.tapestry.IAsset;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.Asset;
import org.apache.tapestry.annotations.Bean;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.bean.EvenOdd;
import org.braintrain.domain.Module;
import org.braintrain.domain.User;
import org.braintrain.services.search.users.SortedUsersQueryFactory;

import java.util.Collections;
import java.util.List;

/**
 * Tapestry UserAdmin page.  This page alows the User Administrator to edit, create or delete
 * users from the BrainTrain user base.
 * 
 * @author John Ericksen
 *
 */

public abstract class UserAdmin extends BrainTrainSecuredBasePage {
    
    @Asset("/images/undo.gif")
    public abstract IAsset getBackImage();
    
    @Bean
    public abstract EvenOdd getEvenOdd();
    
    @Bean
    public abstract EvenOdd getEvenOdd2();
    
    @InjectPage("EditUser")
    public abstract EditUser getEditUserPage();
    
    //loop variables
    public abstract User getUser();
    public abstract void setUser(User u);
    
    public abstract Module getModule();
    public abstract void setModule(Module m);

    /**
     * Returns a list of all the available modules
     * 
     * @return
     */
    public List<Module> getModuleList(){
        List<Module> modules = (List<Module>)getGenericService().find(SimpleSearch.simpleObjectSearch(Module.class, "active", true));
        
        Collections.sort(modules);
        
        return modules;
    }
    
    private List<Module> currentModules = null;
    private boolean rewindOccured = false;
    public List<Module> getModules()
    {
        if(currentModules == null || (rewindOccured && !getRequestCycle().isRewinding() ))
        {
            currentModules = getModuleList();
            rewindOccured = false;
        }
        if(getRequestCycle().isRewinding() && !rewindOccured)
        {
            rewindOccured = true;
            currentModules = getModuleList();
        }
        
        return currentModules;
        
    }
    
    public void saveModules(IRequestCycle cycle)
    {
        if(currentModules != null)
        {
            for(Module module: currentModules)
            {
                getGenericService().saveOrUpdate(module);
                
            }
        }
        //todo:redirect
    }
    
    /**
     * Returns a sorted list of all the users in BrainTrain
     * 
     * @return
     */
    public List<User> getAllUsers()
    {
        return getGenericService().find(new SortedUsersQueryFactory());
    }
    
    /**
     * Forwards to the EditUser page the selected user
     * @param cycle
     */
    public void editUser(IRequestCycle cycle)
    {
        Object[] parameters = cycle.getListenerParameters();
        if(parameters.length > 0 && parameters[0] instanceof Long)
        {
            getEditUserPage().activate(cycle, (Long)parameters[0]);
        }
    }


}
